﻿using System;
using System.Windows.Media;
using ForecastManager.Dialogs;

namespace ForecastManager.Controls
{
	/// <summary>
	/// Interaction logic for SnapshotBrick.xaml
	/// </summary>
	public partial class SnapshotBrick
	{
		/// <summary>
		/// Снимок, отображаемый этим э/у.
		/// </summary>
		private Snapshot _snapshot;

		/// <summary>
		/// Путь для сохранения снимка.
		/// </summary>
		private string _snapshotPath;

		public event EventHandler Select;

		public void OnSelect(EventArgs e)
		{
			EventHandler handler = Select;
			if (handler != null) handler(this, e);
		}

		public SnapshotBrick(Snapshot snapshot, string snapshotPath)
		{
			InitializeComponent();

			_snapshot = snapshot;
			_snapshotPath = snapshotPath;

			UpdateVisualState();
		}

		private void UpdateVisualState()
		{
			ControlVisualRoot.Background = _snapshot.ResultsApplied
			                               	? new SolidColorBrush(Colors.LightGreen)
			                               	: new SolidColorBrush(Colors.LightGoldenrodYellow);

			SnapshotNameTextBlock.Text = _snapshot.SnapshotName;
			SnapshotDateTextBlock.Text = _snapshot.SnapshotDate.ToString("dd.MM.yyyy HH:mm");
		}

		private void ControlVisualRootMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			OnSelect(new EventArgs());
		}

		internal void ClearSelection()
		{
			//визуальное снятие выделения
		}

		public Snapshot GetSnapshot()
		{
			return _snapshot;
		}

		/// <summary>
		/// Загрузка результатов для снимка.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void LoadResultsClick(object sender, System.Windows.RoutedEventArgs e)
		{
			LoadResultDialog loadResultDialog = new LoadResultDialog(_snapshot);
			loadResultDialog.ShowDialog();
			if (loadResultDialog.DialogResult.HasValue && loadResultDialog.DialogResult.Value)
			{
				_snapshot = loadResultDialog.GetSnapshot();
				_snapshot.ResultsApplied = true;
				Snapshot.Serialize(_snapshotPath, _snapshot);

				UpdateVisualState();
			}
		}
	}
}
